﻿@using SmartStore.Web.Models.Catalog;
@model IList<ManufacturerModel>
@{
	Layout = "_Layout";

	Html.AddTitleParts(T("PageTitle.Manufacturers").Text);

	var numericList = Model.Where(x => Char.IsNumber(x.Name.Value[0])).OrderBy(x => x.Name);
	var alphabeticalList = Model.Except(numericList).Where(x => x.Name.HasValue()).OrderBy(x => x.Name);
    var sortManufacturersAlphabetically = (bool)ViewBag.SortManufacturersAlphabetically;
}

<div class="page manufacturer-list-page">
    <div class="page-title">
        <h1 class="h2">@T("Manufacturers.List")</h1>
    </div>
    <div class="page-body">
        @{ Html.RenderWidget("manufacturers_all_top"); }

        @if (sortManufacturersAlphabetically)
        {
            var groupedManus = alphabeticalList.GroupBy(x => char.ToUpper(x.Name.Value[0]));

            <div class="manufacturer-nav">
                <ul class="navbar justify-content-start list-unstyled">                    
                    @if (numericList.Any())
                    {
                        <li class="nav-item"><a class="nav-link" href="#number">@T("Catalog.ManufacturerAll.Numbers")</a></li>
                    }

                    @foreach (var manu in groupedManus)
                    {
                        <li class="nav-item"><a class="nav-link" href="#@manu.Key">@manu.Key</a></li>
                    }
                </ul>
            </div>

            if (numericList.Any())
            {
                <h4 id="number" class="manufacturer-list-title">@T("Catalog.ManufacturerAll.Numbers")</h4>
                <div class="artlist artlist-grid artlist-6-cols artlist-boxed manufacturer-grid">
                    @foreach (var item in numericList)
                    {
                        @RenderManufacturer(item);
                    }
                </div>
            }

            foreach (var manu in groupedManus)
            {
                <h4 id="@manu.Key" class="manufacturer-list-title">@manu.Key</h4>
                <div class="artlist artlist-grid artlist-6-cols artlist-boxed manufacturer-grid">
                    @foreach (var item in manu)
                    {
                        @RenderManufacturer(item);
                    }
                </div>
            }
        }
        else
        {
		    <div class="artlist artlist-grid artlist-6-cols artlist-boxed manufacturer-grid">
			    @foreach (var item in Model)
			    {
                    @RenderManufacturer(item);
                }
		    </div>
        }

        @{ Html.RenderWidget("manufacturers_all_bottom"); }
    </div>
</div>

@helper RenderManufacturer(ManufacturerModel item)
{
    var picture = item.PictureModel;
    var url = Url.RouteUrl("Manufacturer", new { SeName = item.SeName });

    <article class="art" data-id="@item.Id">
        <div class="art-picture-block">
            @if (picture != null && picture.ImageUrl.HasValue())
            {
                <a class="art-picture img-center-container" href="@url" title="@picture.Title">
                    <img src="@picture.ImageUrl" alt="@picture.AlternateText" />
                </a>
            }
        </div>
        <div class="art-genericname">
            <a href="@url" title="@item.Name">
                <span @Html.LanguageAttributes(item.Name)>@item.Name</span>
            </a>
        </div>
    </article>
}

<script type="text/javascript">
    $(function () {
        $('.manufacturer-nav .nav-link').click(function (e) {
        	e.preventDefault();
            $.scrollTo(e.target.getAttribute('href'), 800);
            return false;
        });
    });
</script>